package com.wjk.kylin.id.example.controller;

import com.wjk.kylin.id.core.uid.UidGenerator;
import com.wjk.kylin.id.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/demo")
public class DemoController {
    @Autowired
    @Qualifier("cachedUidGenerator")
    private UidGenerator uidGenerator;

    @GetMapping("/nextId")
    public Long getId(@RequestParam("bizType") String bizType) {
        return IdUtil.nextId(bizType);
    }

    @GetMapping("/nextIds")
    public Long getIds(@RequestParam("bizType") String bizType, @RequestParam("size") int size) {
        for (int i = 0; i < size; i++) {
            log.info("{}", IdUtil.nextId(bizType));
        }
        return 0L;
    }

    @GetMapping("/nextIdBatchSize")
    public Long getIdBatchSize(@RequestParam("bizType") String bizType, @RequestParam("batchSize") Integer batchSize) {
        List<Long> idList = IdUtil.nextId(bizType, batchSize);
        for (Long id : idList) {
            log.info("{}", id);
        }
        return 0L;
    }

    @GetMapping("/uid")
    public Long getUId(@RequestParam("batchSize") Integer batchSize) {
        for (int i = 0; i < batchSize; i++) {
            log.info("{}", uidGenerator.getUID());
        }
        return 0L;
    }

}
